package com.hualan.servlet.goodsServlet;

import com.alibaba.fastjson.JSON;
import com.hualan.bean.JsonResult;
import com.hualan.enums.ResponseEnum;
import com.hualan.service.GoodsService;
import com.hualan.service.impl.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;

@WebServlet("/goods/modifyShopGoods")
public class GoodsUpdateStatusServlet extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try{
			GoodsService service = new GoodsServiceImpl();
			// TODO：解析JSON为Map
			BufferedReader reader = req.getReader();
			String line;
			StringBuilder builder = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				builder.append(line);
			}
			// TODO: 封装Map
			HashMap<String,Integer> hashMap = JSON.parseObject(builder.toString(), HashMap.class);
			int i = service.modifyShopStatus(String.valueOf(hashMap.get("goodsStatus")), String.valueOf(hashMap.get("goodsId")));
			if (i > 0){
				resp.getWriter().write(JSON.toJSONString(JsonResult.success(200,"修改成功")));
			} else {
				resp.getWriter().write(JSON.toJSONString(JsonResult.error(ResponseEnum.INCORRECT_PARAMS,"修改失败")));
			}
		}catch (Exception e){
			e.printStackTrace();
			resp.getWriter().write(JSON.toJSONString(JsonResult.error(500,"系统异常")));
		}
		
	}
}
